package com.heima.controller;

import com.heima.common.result.PageResult;
import com.heima.common.result.Result;
import com.heima.pojo.dto.PageDTO;
import com.heima.pojo.entity.Partner;
import com.heima.pojo.vo.PartnerVO;
import com.heima.service.PartnerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * \* User: chy
 * \* Date: 2025/8/17  9:59
 * \* Description:
 * \
 */
@RestController
@RequestMapping("api/user-service/partner")
@Slf4j
public class PartnerController {

    @Autowired
    private PartnerService partnerService;

    /**
     * 合作商分页查询
     * @param partnerDTO
     * @return
     */
    @GetMapping("search")
    public PageResult<PartnerVO> page(PageDTO partnerDTO) {
        log.info("合作商搜索{}",partnerDTO);
        return partnerService.page(partnerDTO);
    }
  /**
     * 新增合作商
     * @param partner
     * @return
     */
    @PostMapping
    public Result add(@RequestBody Partner partner) {
        log.info("新增合作商{}",partner);
        partnerService.add(partner);
        return Result.success();
    }

    /**
     *  查询合作商
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<Partner> getByPartner(@PathVariable Integer id){
        log.info("供应商id:{}",id);
      Partner  partner = partnerService.getById(id);
        return Result.success(partner);
    }
   /**
     * 修改合作商
     * @param partner
     * @return
     */
    @PutMapping("/{id}")
    public Result update(@PathVariable Integer id ,@RequestBody Partner partner){
        log.info("修改的供货商id为:{}",id);
        partner.setId(id);
        partnerService.updateById(partner);
        return Result.success();
    }
   /**
     * 删除合作商
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public  Result deleteById(@PathVariable Integer id){
        log.info("需要删除的合作商id:{}",id);
        partnerService.deleteById(id);
       return Result.success();
    }

    /**
     * 查询合作商数量
     * @return
     */
    @GetMapping("/count")
    public Integer countPartner(){
        return partnerService.count();
    }
}
